package com.luo.bbs.front.webapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.bbs.front.service.IForAdminService;
import com.luo.bbs.front.webapi.mapper.*;
import com.luo.bbs.front.webapi.pojo.UserLoginPrincipal;
import com.luo.common.exception.BBSServiceException;
import com.luo.common.restful.JsonPage;
import com.luo.common.restful.ResponseCode;
import com.luo.pojo.Announce;
import com.luo.pojo.Building;
import com.luo.pojo.Repair;
import com.luo.pojo.advice.entity.Advice;
import com.luo.pojo.comment.entity.Comment;
import com.luo.pojo.comment.vo.CommentVO;
import com.luo.pojo.question.entity.Question;
import com.luo.pojo.question.vo.QuestionVO;
import com.luo.pojo.tag.entity.Tag;
import com.luo.pojo.ums.entity.User;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@DubboService
public class ForAdminServiceImpl implements IForAdminService {
    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private AdviceMapper adviceMapper;

    @Autowired
    private BuildingMapper buildingMapper;

    @Autowired
    private AnnounceMapper announceMapper;

    @Autowired
    private RepairMapper repairMapper;

    @Override
    public int checkQuestion(Integer id) {
        if (id == null) {
            throw new BBSServiceException(ResponseCode.NOT_FOUND, "参数不能为空");
        }
        Question question = questionMapper.selectById(id);
        if (question == null) {
            throw new BBSServiceException(ResponseCode.NOT_FOUND, "帖子不存在！");
        }
        //点击审核帖子，如果已经审核，修改为未审核，反之；
        if (question.getStatus() == 1) {
            question.setStatus(0);
        } else {
            question.setStatus(1);
        }
        int row = questionMapper.updateById(question);
        if (row != 1) {
            throw new BBSServiceException(ResponseCode.INTERNAL_SERVER_ERROR, "系统繁忙");
        }
        return question.getStatus();
    }

    @Override
    public int checkComment(String id) {
        if (id == null) {
            throw new BBSServiceException(ResponseCode.NOT_FOUND, "参数不能为空");
        }
        Comment comment = commentMapper.selectById(id);
        if (comment == null) {
            throw new BBSServiceException(ResponseCode.NOT_FOUND, "帖子不存在！");
        }
        //点击审核评论，如果已经审核，修改为未审核，反之；
        if (comment.getStatus() == 1) {
            comment.setStatus(0);
        } else {
            comment.setStatus(1);
        }
        int row = commentMapper.updateById(comment);
        if (row != 1) {
            throw new BBSServiceException(ResponseCode.INTERNAL_SERVER_ERROR, "系统繁忙");
        }
        return comment.getStatus();
    }

    //查询所有帖子
    @Override
    public List<QuestionVO> getAll() {
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("delete_status", 0);
        List<Question> questions = questionMapper.selectList(queryWrapper);
        List<QuestionVO> list = new ArrayList<>();
        for (Question question : questions) {
            QuestionVO questionVO = new QuestionVO();
            BeanUtils.copyProperties(question, questionVO);
            list.add(questionVO);
        }
        return list;
    }

    //未审核帖子
    @Override
    public List<QuestionVO> getAllNotPass() {
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("delete_status", 0).eq("status",0);
        List<Question> questions = questionMapper.selectList(queryWrapper);
        List<QuestionVO> list = new ArrayList<>();
        for (Question question : questions) {
            QuestionVO questionVO = new QuestionVO();
            BeanUtils.copyProperties(question, questionVO);
            list.add(questionVO);
        }
        return list;
    }

    //查询版块对应帖子
    @Override
    public List<QuestionVO> getByTag(Integer id) {
        QueryWrapper<Question> qw = new QueryWrapper<>();
        qw.eq("tag_id", id);
        List<Question> questions = questionMapper.selectList(qw);
        if (questions.size() == 0 || questions.isEmpty()) {
            return null;
        }
        List<QuestionVO> list = new ArrayList<>();
        for (Question question : questions) {
            QuestionVO questionVO = new QuestionVO();
            BeanUtils.copyProperties(question, questionVO);
            list.add(questionVO);
        }
        return list;
    }

    //查询所有评论
    @Override
    public List<CommentVO> getAllCom() {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("delete_status", 1);
        List<Comment> comments = commentMapper.selectList(queryWrapper);
        if (comments == null) {
            return null;
        }
        List<CommentVO> list = new ArrayList<>();
        for (Comment comment : comments) {
            CommentVO commentVO = new CommentVO();
            BeanUtils.copyProperties(comment, commentVO);
            list.add(commentVO);
        }
        return list;
    }

    //未审核评论
    @Override
    public List<CommentVO> getAllComNotPass() {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("delete_status", 1).eq("status",0);
        List<Comment> comments = commentMapper.selectList(queryWrapper);
        if (comments == null) {
            return null;
        }
        List<CommentVO> list = new ArrayList<>();
        for (Comment comment : comments) {
            CommentVO commentVO = new CommentVO();
            BeanUtils.copyProperties(comment, commentVO);
            list.add(commentVO);
        }
        return list;
    }

    //根据ID删除帖子
    @Transactional
    @Override
    public void removeQuestion(Integer id) {
        if (id == null) {
            throw new BBSServiceException(ResponseCode.BAD_REQUEST, "参数不能为空！");
        }
        Question question = questionMapper.selectById(id);
        if (question == null) {
            throw new BBSServiceException(ResponseCode.NOT_FOUND, "帖子不存在");
        }
        //逻辑删除
        question.setDeleteStatus(1);
        questionMapper.updateById(question);
        //删除该帖子下的评论
        QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();
        commentQueryWrapper.eq("question_id", id);
        List<Comment> comments = commentMapper.selectList(commentQueryWrapper);
        if (comments.size() > 0) {
            for (Comment comment : comments) {
                comment.setDeleteStatus(0);
                commentMapper.updateById(comment);
            }
        }
    }

    //根据ID删除评论
    @Override
    public void removeComment(String id) {
        if (id == null) {
            throw new BBSServiceException(ResponseCode.BAD_REQUEST, "参数不能为空！");
        }
        Comment comment = commentMapper.selectById(id);
        if (comment == null) {
            throw new BBSServiceException(ResponseCode.NOT_FOUND, "帖子不存在");
        }
        //逻辑删除
        comment.setDeleteStatus(0);
        commentMapper.updateById(comment);

        //删除该评论下的子评论
        QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();
        commentQueryWrapper.eq("parent_comment_id", id);
        List<Comment> comments = commentMapper.selectList(commentQueryWrapper);
        if (comments.size() > 0) {
            for (Comment sonComment : comments) {
                sonComment.setDeleteStatus(0);
                commentMapper.updateById(sonComment);
            }
        }
    }

    //根据ID删除版块
    @Override
    public void removeTag(Integer id) {
        if (id == null) {
            throw new BBSServiceException(ResponseCode.BAD_REQUEST, "参数不能为空！");
        }
        Tag tag = tagMapper.selectById(id);
        if (tag == null) {
            throw new BBSServiceException(ResponseCode.NOT_FOUND, "帖子不存在");
        }
        //逻辑删除
        tag.setDeleteStatus(1);
        tagMapper.updateById(tag);
    }

    //添加版块
    @Override
    public void addTag(Tag tag) {
        if (tag.getName() == null) {
            throw new BBSServiceException(ResponseCode.BAD_REQUEST, "参数不能为空！");
        }
        QueryWrapper<Tag> wrapper = new QueryWrapper<>();
        Tag selectOne = tagMapper.selectOne(wrapper.eq("name", tag.getName()));
        if (selectOne != null) {
            throw new BBSServiceException(ResponseCode.CONFLICT, "该版块已存在！");
        }
        int insert = tagMapper.insert(tag);
        if (insert != 1) {
            throw new BBSServiceException(ResponseCode.INTERNAL_SERVER_ERROR, "系统繁忙！");
        }
    }

    //编辑版块
    public void editTag(Tag tag) {
        if (tag.getName() == null) {
            throw new BBSServiceException(ResponseCode.BAD_REQUEST, "参数为空");
        }
        Tag selectById = tagMapper.selectById(tag.getId());
        if (selectById == null) {
            throw new BBSServiceException(ResponseCode.NOT_FOUND, "修改的版块不存在");
        }
        tag.setGmtModified(LocalDateTime.now());
        tagMapper.updateById(tag);
    }

    //查询某一条帖子的评论
    @Override
    public List<CommentVO> getCommentByQ(Integer id) {
        if (id == null) {
            throw new BBSServiceException(ResponseCode.BAD_REQUEST, "参数不能为空");
        }
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("question_id", id);
        List<Comment> list = commentMapper.selectList(wrapper);
        if (list == null) {
            return null;
        }
        List<CommentVO> commentVOList = new ArrayList<>();
        for (Comment comment : list) {
            CommentVO commentVO = new CommentVO();
            BeanUtils.copyProperties(comment, commentVO);
            commentVOList.add(commentVO);
        }
        return commentVOList;
    }

    @Override
    public int disposeAdvice(Integer id) {
        if (id == null) {
            throw new BBSServiceException(ResponseCode.BAD_REQUEST, "参数不难为空");
        }
        Advice advice = adviceMapper.selectById(id);
        if (advice == null) {
            throw new BBSServiceException(ResponseCode.NOT_FOUND, "意见不存在");
        }
        if (advice.getStatus()==1){
            advice.setStatus(0);
            adviceMapper.updateById(advice);
            return 0;
        }
        advice.setStatus(1);
        adviceMapper.updateById(advice);
        return 1;
    }

    @Override
    public List<Advice> getAdvice() {
        List<Advice> advice = adviceMapper.selectList(null);
        return advice;
    }

    @Override
    public List<Advice> getAdviceNot() {
        return adviceMapper.selectList(
                new QueryWrapper<Advice>().eq("status",0));
    }

    @Override
    public List<Building> getBuildings() {
        List<Building> buildings = buildingMapper.selectList(null);
        return buildings;
    }

    @Override
    public int enableBuilding(Integer id) {
        if (id == null){
            throw new BBSServiceException(ResponseCode.BAD_REQUEST,"参数错误");
        }
        Building building = buildingMapper.selectById(id);
        if (building==null){
            throw new BBSServiceException(ResponseCode.BAD_REQUEST.NOT_FOUND,"不存在");
        }
        if (building.getEnabled()==1){
            building.setEnabled(0);
            buildingMapper.updateById(building);
            return 0;
        }
        QueryWrapper<Building> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enabled",1)
                    .eq("invite_code",building.getInviteCode());
        List<Building> buildings = buildingMapper.selectList(queryWrapper);
        if (buildings.size()>0){
            throw new BBSServiceException(ResponseCode.CONFLICT,"已存在");
        }
        building.setEnabled(1);
        buildingMapper.updateById(building);
        return 1;
    }

    @Override
    public void addBuilding(Building building) {
        if (building.getName()==null || building.getInviteCode()==null){
            throw new BBSServiceException(ResponseCode.BAD_REQUEST,"参数错误");
        }
        QueryWrapper<Building> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enabled",1).eq("invite_code",building.getInviteCode());
        List<Building> buildings = buildingMapper.selectList(queryWrapper);
        if (buildings.size()>0){
            throw new BBSServiceException(ResponseCode.CONFLICT,"已存在");
        }
        buildingMapper.insert(building);
    }

    @Override
    public void addAnnounce(Announce announce) {
        if (announce.getContent()==null){
            throw new BBSServiceException(ResponseCode.BAD_REQUEST,"参数错误");
        }
        announceMapper.insert(announce);
    }

    @Override
    public List<Announce> getAnnounceList() {
        List<Announce> announceList = announceMapper.selectList(
                new QueryWrapper<Announce>().orderByDesc("gmt_create")
        );
        return announceList;
    }

    @Override
    public List<Repair> getRepairAll() {
        return repairMapper.selectList(new QueryWrapper<Repair>()
                .orderByAsc("gmt_create"));
    }

    //查看未处理的报修
    @Override
    public List<Repair> getRepair() {
        return repairMapper.selectList(new QueryWrapper<Repair>()
        .orderByAsc("gmt_create").eq("status",0));
    }

    @Override
    public int updateRepair(Integer id) {
        Repair repair = repairMapper.selectById(id);
        if (repair == null){
            throw new BBSServiceException(ResponseCode.NOT_FOUND,"不存在");
        }
        if (repair.getStatus()==1){
            repair.setStatus(0);
            repairMapper.updateById(repair);
            return 0;
        }
        repair.setStatus(1);
        repairMapper.updateById(repair);
        return 1;
    }


}
